﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class AB55AV95 : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI
        {
            get
            {
                return (INTEXDataSet)TryFindResource("iNTEXDataSet");
            }
        }
        public INTEXDataSet setWorker=new INTEXDataSet();
        AB55AV95Data m_Data, m_DataX;
        public DataSet setResult;
        
        private DataRow dwUIABPOS; 
        public AB55AV95()
        {
            InitializeComponent();
            this.Name = "织造生产状态报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();          

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
         
            m_Data = new AB55AV95Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }
        #endregion
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();
                
                dwUIABPOS["ABPAUFART"] = "PR";
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();
                string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
                if (ERLKZ == "True")
                {
                    dwUIABPOS["ABPERLKZ"] = "J";
                }
                else
                {
                    dwUIABPOS["ABPERLKZ"] = " ";
                }
                dwUIABPOS["ABPAUFKRS"] = KRS;
                dwUIABPOS["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUIABPOS["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }

                dwUIABPOS["ABPZUGET"] = "0";
                dwUIABPOS["ABPART"] = txtArticle.Text.ToString().Trim();
                /*string FACTORY = txtFACTORY.Text.ToString().Trim();
                if (FACTORY.Trim().Length > 0)
                {
                    dwUIABPOS["LSNAME1"] = m_Data.Right("000000" + FACTORY, 8);
                }*/
                setResult = m_Data.LoadABPOS(dwUIABPOS);
                setUI.Merge(setResult);
                totalrecord = setUI.ABPOS.Rows.Count;

                foreach (DataRow dwNABPOS in setUI.ABPOS)
                {
                    /*m_Data = new AB55AV95Data();
                    m_Data.OracleDB = this.OracleDB;
                    m_Data.proxy = this.proxy;
                    m_Data.ThreadID = ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage(m_Data, dwNABPOS);*/
                    ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage("AB55AV95Data", dwNABPOS, OracleDB, proxy, null);
                }
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUIABPOS = setUI.ABPOS.NewRow();
                
                dwUIABPOS = setUI.ABPOS.NewRow();
                if (setUI != null)
                {
                    //setResult = m_Data.GetWASTAMM();
                    //setUI.Merge(setResult);
                }
                 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        } 
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;  
                     
                }
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_DataX = new AB55AV95Data(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                AB55AV95Report m_rpt = new AB55AV95Report(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL"); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
